package com.mark.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.util.AjaxResponse;
import com.common.util.JpaPageable;
import com.common.util.TablePage;
import com.exam.entity.Exam;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mark.entity.StuAnswer;
import com.mark.entity.StuOptions;
import com.mark.entity.dto.StuAnswerDTO;
import com.mark.service.IStuAnswerService;
import com.paper.entity.Subtopics;
import com.question.entity.Single;
import com.user.entity.dto.StudentDTO;
import com.user.entity.dto.TeacherDTO;

@Controller
@RequestMapping("/stuAnswer")
public class StuAnswerController {
	@Autowired
	private IStuAnswerService stuAnswerService;
	
	@RequestMapping("/findPage")
	public @ResponseBody TablePage<StuAnswer> findPage(HttpSession session,JpaPageable pageable){
		try {
			StudentDTO student = (StudentDTO) session.getAttribute("student");
			if(null==student)return null;
			return stuAnswerService.findPage(StuAnswerDTO.getSpecification(student.getId()), pageable.getPageable());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/selectAnswer")
	public @ResponseBody AjaxResponse selectAnswer(HttpSession session,String stuOptions) {
		try {
			ObjectMapper objectMapper = new ObjectMapper();
			StuOptions obj = objectMapper.readValue(stuOptions,StuOptions.class);
			stuAnswerService.selectAnswer(obj);
			return new AjaxResponse(true, "作答成功!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/loadAnswer")
	public @ResponseBody StuAnswer loadAnswer(HttpSession session,Long eId) {
		try {
			StudentDTO student = (StudentDTO) session.getAttribute("student");
			if(null==student)return null;
			return stuAnswerService.findByStuAndExam(student.getId(),eId);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/commit")
	public @ResponseBody AjaxResponse commit(Long stuAnswerId,boolean isTimeUp) {
		try {
			if(isTimeUp) {
				stuAnswerService.commitAnswer(stuAnswerId);
				return new AjaxResponse(true, "考试时间到，考试结束。");
			}else {
				if(stuAnswerService.isTimeLess(stuAnswerId))
					return new AjaxResponse(false, "离考试结束大于30分钟，不能交卷!");
				else {
					stuAnswerService.commitAnswer(stuAnswerId);
					return new AjaxResponse(true, "交卷成功!");
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return new AjaxResponse(false, "操作失败...");
		}
	}
	@RequestMapping("/getTaskNum")
	public @ResponseBody Integer getTaskNum(HttpSession session) {
		try {
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto)
				return stuAnswerService.findTaskNum(dto.getId());
			else return null;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findTask")
	public @ResponseBody List<Exam> findTask(HttpSession session){
		try {
			TeacherDTO dto = (TeacherDTO) session.getAttribute("teacher");
			if(null!=dto) {
				return stuAnswerService.findTask(dto.getId());
			}else {
				return null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping("/findUnMarkedList")
	public @ResponseBody List<StuAnswer> findUnMarkedList(Long exam_id){
		try {
			if(null!=exam_id) {
				return stuAnswerService.findUnMarkList(exam_id);
			}else {
				return null;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}
	@RequestMapping(value="/startMark",produces="application/json;charset=utf-8")
	public @ResponseBody String startMark(Long exam_id) {
		try {
			if(null!=exam_id) {
				return stuAnswerService.startMarked(exam_id);
			}else {
				return "{\"success\":false,\"msg\":\"没有该试卷信息!\"}";
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "{\"success\":false,\"msg\":\"出错了...!\"}";
		}
	}
	@RequestMapping(value="/getTask",produces="application/json;charset=utf-8")
	public @ResponseBody String getTask(Long exam_id) {
		try {
			return stuAnswerService.getTask(exam_id);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "{\"num\":-1}";
		}
	}
	@RequestMapping(value="/markOne",produces="application/json;charset=utf-8")
	public @ResponseBody String markOne(String stuAnswer) {
		try {
			System.out.println(stuAnswer);
			ObjectMapper objectMapper = new ObjectMapper();
			StuAnswer obj = objectMapper.readValue(stuAnswer,StuAnswer.class);
			return stuAnswerService.markOne(obj);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return "{\"num\":-1}";
		}
	}
}
